package com.cysion.study.ui

import android.view.View
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.cysion.study.ktbox.base.BaseActivity
import com.cysion.study.ktbox.image.RoundTransform
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.study.ktbox.utils.whiteTextTheme
import com.cysion.other.color
import com.cysion.targetfun._subscribe
import com.cysion.study.uibox.bar.TopBar
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.R
import com.cysion.study.entity.SeniorityEntity
import com.cysion.study.helper.UserCache
import com.cysion.study.net.ApiClient
import com.cysion.study.widget.ShowToast
import kotlinx.android.synthetic.main.activity_teacher_info.*

//教师资历
class TeacherInfoActivity : BaseActivity(), View.OnClickListener {


    override fun getLayoutId(): Int = R.layout.activity_teacher_info


    var obj: SeniorityEntity? = null

    override fun initView() {
        whiteTextTheme(color(R.color.main_cl))
        topbar.apply {
            setTitle("教师资历")
            setOnTopBarClickListener { obj, pos ->
                if (pos == TopBar.Pos.LEFT) {
                    finish()
                }
            }
        }
        tv_submit.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        var name = et_name.text.toString().trim()
        var age=et_time.text.toString().trim()
        var undergo=et_jl.text.toString().trim()
        var synopsis = et_jj.text.toString().trim()
        var phone = et_phone.text.toString().trim()

        var img= obj!!.headImageUrl

        Alert.loading(self)
        ApiClient.api.senioritySaveOrUpdate(UserCache.token,name,img,age,synopsis,undergo,phone).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()
                    ShowToast("提交成功")
                    finish()
                }
                _onError {
                    Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                }
            }


    }

    override fun initData() {
        super.initData()
        getDataList()

    }

    private fun getDataList() {
        Alert.loading(self)
        ApiClient.api.seniorityGetInfo(UserCache.token).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()
                    obj = it
                    inView(it)

                }
                _onError {
                    Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                }
            }

    }

    private fun inView(it: SeniorityEntity?) {
        Glide.with(this)
            .load(it?.headImageUrl)
            .apply(RequestOptions.placeholderOf(R.mipmap.place_holder_big))
            .apply(RequestOptions.bitmapTransform(RoundTransform(6)))
            .into(iv_head)
        et_name.setText(it?.name)
        et_time.setText(it?.age)//教龄
        et_jl.setText(it?.undergo)//经历
        et_jj.setText(it?.synopsis)//简介
        et_phone.setText(it?.phone)
    }

    override fun closeMvp() {
    }

}
